
package com.client;
import java.util.ArrayList;
import java.util.List;

import com.client.data.PropositionListGrid;
import com.client.service.UserManagementService;
import com.client.service.UserManagementServiceAsync;
import com.client.service.data.AssureDataService;
import com.client.service.data.AssureDataServiceAsync;
import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.shared.model.Assure;
import com.shared.model.Produit;
import com.shared.model.Proposition;
import com.smartgwt.client.widgets.grid.events.CellClickEvent;
import com.smartgwt.client.widgets.grid.events.CellClickHandler;

	public class PanelTarif extends Composite {

		private final AssureDataServiceAsync assureService = GWT.create(AssureDataService.class);
		private final UserManagementServiceAsync userService = GWT.create(UserManagementService.class);
		
		private PropositionListGrid propositionListGrid = new PropositionListGrid();		
		List<Proposition> listePropositions = new ArrayList<Proposition>() ;
		private int selectedIndex = 0;
		private Produit prodSelec = null;
		
		private HTML msgHtml = new HTML();
		private Button buttonValider = new Button("Accepter");
		
		private Label labelProd = new Label();
		private PanelPaypal paypal = new PanelPaypal() ;
		
		
		public PanelTarif(){
			
			initWidget(createPanelTarif());
			
			loadList();
			
			
			propositionListGrid.addCellClickHandler(new CellClickHandler() {	
				
				public void onCellClick(CellClickEvent event) {
					selectedIndex = event.getRowNum();	
					getProduitPropose();
				
				}
			});
			
			buttonValider.addClickHandler(new ClickHandler() {				
				public void onClick(ClickEvent event) {
					acceptProposition();					
				}
			});
			
			paypal.getButtonPayer().addClickHandler(new ClickHandler() {
				
				public void onClick(ClickEvent event) {
					//acceptProposition();
					paypal.getForm().submit();
				}
			});
			
			
			
		}
		
		public HorizontalPanel createPanelTarif(){
			
			final HorizontalPanel mainPanel = new HorizontalPanel();
			mainPanel.setSpacing(5);
			VerticalPanel panelList = new VerticalPanel();
			Label labelTarif = new Label("Propositions :");
			labelTarif.setStylePrimaryName("titre");
			panelList.add(labelTarif);
			panelList.add(propositionListGrid);
			mainPanel.add(panelList);
			
			labelProd.setVisible(false);
			msgHtml.setVisible(false);
			buttonValider.setVisible(false);
			paypal.setVisible(false);
			
			VerticalPanel panelProp = new VerticalPanel();
			panelProp.setSpacing(5);
			panelProp.add(labelProd);
			panelProp.add(msgHtml);
			panelProp.add(paypal);
			panelProp.add(buttonValider);
			mainPanel.add(panelProp);
			
			return mainPanel ;
			
		}
		
		public void loadList(){
			listePropositions.clear();
			propositionListGrid.selectAllRecords();
			propositionListGrid.removeSelectedData();
			userService.getAssureCourant(new AsyncCallback<Assure>() {
				public void onFailure(Throwable caught) {
					Window.alert(caught.getMessage()) ;					
				}
				
				public void onSuccess(Assure result) {
					assureService.getListProposition(result, new AsyncCallback<List>() {
						public void onFailure(Throwable caught) {					
							Window.alert(caught.getMessage()) ;
						}
						public void onSuccess(List listresult) {	
							listePropositions = listresult;
							for (Proposition p : (List<Proposition>) listresult) {
								propositionListGrid.addData(p); 
							}
						}
					});					
				}
			});
		}
		
		public void acceptProposition(){
			if(listePropositions.get(selectedIndex)!=null){
			userService.getAssureCourant(new AsyncCallback<Assure>() {
				public void onFailure(Throwable caught) {
					Window.alert(caught.getMessage()) ;					
				}
				
				public void onSuccess(Assure result) {
					assureService.acceptProposition(result, listePropositions.get(selectedIndex), new AsyncCallback<String>() {
						
						public void onSuccess(String result) {
							loadList();							
						}
						
						public void onFailure(Throwable caught) {
							Window.alert(caught.getMessage()) ;			
							
						}
					});
						
				}
			});
			}
		}
		
		public void getProduitPropose(){
			if(!listePropositions.get(selectedIndex).isAccepted()){
			userService.getAssureCourant(new AsyncCallback<Assure>() {
				public void onFailure(Throwable caught) {
					Window.alert(caught.getMessage()) ;					
				}
				
				public void onSuccess(Assure result) {
						assureService.getProduitPropose(result,listePropositions.get(selectedIndex), new AsyncCallback<Produit>() {
						
						public void onSuccess(Produit result) {
							prodSelec = result;	
							if(prodSelec!=null){								
								labelProd.setText("Produit : "+result.getNom() + " prix : "+ result.getTarification().getMontantTotal());
								labelProd.setVisible(true);
								
								paypal.update(new Object(), String.valueOf(result.getTarification().getMontantTotal()));
								paypal.setVisible(true);	
								
								msgHtml.setVisible(false);
								buttonValider.setVisible(false);
							}else{								
								msgHtml.setHTML(listePropositions.get(selectedIndex).getMessage());
								msgHtml.setVisible(true);
								labelProd.setText("");
								labelProd.setVisible(false);
								paypal.setVisible(false);
								buttonValider.setVisible(true);
							}
							
						}						
						public void onFailure(Throwable caught) {
							Window.alert(caught.getMessage()) ;	
						}
					});			
				}
			});
			}else{
				labelProd.setVisible(false);
				msgHtml.setVisible(false);
				buttonValider.setVisible(false);
				paypal.setVisible(false);
			}
		}
	
}
